<template>
	<view class="content">
		<heade title="设置" cross bg_color="bg_fff"></heade>
		<view class="m-l20 m-t15	">
			<view class="d-flex align-center">
				<view class="f-s14" style="width:178rpx;">默认计量单位:</view>
				<view class="m-l15">
					<picker @change="bindPickerChange" class="flex1" :value="index" :range="array" range-key="name">
						<view class="d-flex align-center justify-content-space-between p-l10 p-r10 p-t5	 p-b5" style="border: 2rpx solid #E2E2E2;border-radius: 10rpx;width: 280rpx;">
							<view class="uni-input text-center f-s12">{{array[index].name}}</view>
							<view class="iconfont icon-sanjiaoxing f-s12"></view>
						</view>
					</picker>
				</view>
			</view>
			<view class="d-flex align-center m-t15">
				<view class="f-s14" style="width:178rpx;">订单默认仓库:</view>
				<view class="m-l15">
					<picker @change="warehouseChange" class="flex1" :value="warehouse" :range="warehouseList" range-key="Name">
						<view class="d-flex align-center justify-content-space-between p-l10 p-r10 p-t5	 p-b5" v-if="warehouseList.length > 0" style="border: 2rpx solid #E2E2E2;border-radius: 10rpx;width: 280rpx;">
							<view class="uni-input text-center f-s12">{{warehouseList[warehouse].Name}}</view>
							<view class="iconfont icon-sanjiaoxing f-s12"></view>
						</view>
					</picker>
				</view>
			</view>
			<view class="d-flex align-center m-t15">
				<view class="f-s14" style="width:178rpx;">退货默认仓库:</view>
				<view class="m-l15">
					<picker @change="chargeback_warehouse_codeChange" class="flex1" :value="chargeback_warehouse_code" :range="warehouseList" range-key="Name">
						<view class="d-flex align-center justify-content-space-between p-l10 p-r10 p-t5	 p-b5" v-if="warehouseList.length > 0" style="border: 2rpx solid #E2E2E2;border-radius: 10rpx;width: 280rpx;">
							<view class="uni-input text-center f-s12">{{warehouseList[chargeback_warehouse_code].Name}}</view>
							<view class="iconfont icon-sanjiaoxing f-s12"></view>
						</view>
					</picker>
				</view>
			</view>
			<view class="d-flex align-center m-t15">
				<view class="f-s14" style="width:178rpx;">客户范围:</view>
				<view class="m-l15">
					<picker @change="customerChange" class="flex1" :value="customer" :range="customerList" range-key="name">
						<view class="d-flex align-center justify-content-space-between p-l10 p-r10 p-t5	 p-b5" style="border: 2rpx solid #E2E2E2;border-radius: 10rpx;width: 280rpx;">
							<view class="uni-input text-center f-s12">{{customerList[customer].name}}</view>
							<view class="iconfont icon-sanjiaoxing f-s12"></view>
						</view>
					</picker>
				</view>
			</view>
			<view class="d-flex align-center m-t15">
				<view class="f-s14" style="width:178rpx;">商品范围:</view>
				<view class="m-l15">
					<picker @change="commodityChange" class="flex1" :value="commodity" :range="commodityList" range-key="name">
						<view class="d-flex align-center justify-content-space-between p-l10 p-r10 p-t5	 p-b5" style="border: 2rpx solid #E2E2E2;border-radius: 10rpx;width: 280rpx;">
							<view class="uni-input text-center f-s12">{{commodityList[commodity].name}}</view>
							<view class="iconfont icon-sanjiaoxing f-s12"></view>
						</view>
					</picker>
				</view>
			</view>
			
			<view class="d-flex align-center m-t15">
				<view class="f-s14" style="width:178rpx;">登录状态:</view>
				<view class="bg_FF0000 m-l15 text-center f-s14 co_fff" style="width: 324rpx;height: 60rpx;line-height: 60rpx;border-radius: 60rpx;" @tap="logOut">退出登录</view>
			</view>
		</view>
		<view class="b-liuhai" ><view style="height: 84rpx;"></view></view>
		<view style="position: fixed; bottom: 0;width: 100%;z-index: 9;" class="bg_fff">
			<view class="b-liuhai">
				<view class="bg_3A8CFF d-flex align-center justify-content-center" style="height: 84rpx;" @tap="submit">
					<view class="co_fff f-s14 f-w400" >保存</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		executeSQL,
	} from '@/common/sqlite.js';
	export default {
		data() {
			return {
				index:0,
				warehouse:0,
				customer:0,
				commodity:0,
				chargeback_warehouse_code:7,
				array:[
					{id:1,name:'主计量单位'},
					{id:2,name:'辅计量单位'},
				],
				warehouseList:[{Name:'主仓'},{Name:'主仓'},{Name:'主仓'},{Name:'主仓'},{Name:'主仓'},{Name:'主仓'},{Name:'主仓'},{Name:'周转仓'}],
				customerList:[
					{id:0,name:'所有客户'},
					{id:1,name:'合伙人客户'},
				],
				commodityList:[
					{id:0,name:'所有商品'},
					{id:1,name:'合伙人商品'},
				],
				userInfo:{}
			}
		},
		onLoad() {
			console.log('是否打开数据库');
			var isOpen = plus.sqlite.isOpenDatabase({
				name: 'xiaoap', //数据库的名字  
				path: '_doc/xiaoap.db' //地址  
			}); 
			this.dbInit();
		},
		methods: {
			dbInit: function() {
				executeSQL('xiaoap', 'select * from warehouse', res => {
					// console.log("建筑物查询结果:" + JSON.stringify(res));
					this.warehouseList = res
					if(this.$db.get('default_config')) {
						let valueObj = this.$db.get('default_config')
						this.warehouseList.forEach((e,i) => {
							if(e.Code == valueObj.warehouse_code) {
								this.warehouse = i
							}
							if(e.Code == valueObj.chargeback_warehouse_code) {
								this.chargeback_warehouse_code = i
							}
						})
						this.array.forEach((e,i) => {
							if(e.id == valueObj.unit) {
								this.index = i
							}
						})
						this.customerList.forEach((e,i) => {
							if(e.id == valueObj.partner_scope) {
								this.customer = i
							}
						})
						this.commodityList.forEach((e,i) => {
							if(e.id == valueObj.inventory_scope) {
								this.commodity = i
							}
						})
					}
				})
			},
			// 获取默认下单设置
			getData(){
				this.$http.get('valet_order/default_config','',true).then(res => {
					if(res.code == 1) {
						this.$db.set('default_config',res.data)
						if(res.data.id) {
							this.array.forEach((e,i) => {
								if(e.id == res.data.unit) {
									this.index = i
								}
							})
							this.warehouseList.forEach((e,i) => {
								if(e.Code == res.data.warehouse_code) {
									this.warehouse = i
								}
								if(e.Code == res.data.chargeback_warehouse_code) {
									this.chargeback_warehouse_code = i
								}
							})
							this.customerList.forEach((e,i) => {
								if(e.id == res.data.partner_scope) {
									this.customer = i
								}
							})
							this.commodityList.forEach((e,i) => {
								if(e.id == res.data.inventory_scope) {
									this.commodity = i
								}
							})
						}
						this.userInfo = res.data
					}
				})
			},
			submit() {
				let resData = {
					unit:this.array[this.index].id,
					warehouse_code:this.warehouseList[this.warehouse].Code,
					partner_scope:this.customerList[this.customer].id,
					chargeback_warehouse_code:this.warehouseList[this.chargeback_warehouse_code].Code,
					inventory_scope:this.commodityList[this.commodity].id,
				}
				this.$common.show('设置成功')
				this.$db.set('default_config',resData)
				return
				this.$http.post('valet_order/default_config',resData).then(res => {
					if(res.code == 1) {
						this.$common.show('设置成功'),
						setTimeout(() => {
							this.getData()
						},1000)
					}
				})
			},
			bindPickerChange(e) {
				this.index = e.detail.value
			},
			warehouseChange(e) {
				this.warehouse = e.detail.value
			},
			customerChange(e) {
				this.customer = e.detail.value
			},
			commodityChange(e) {
				this.commodity = e.detail.value
			},
			chargeback_warehouse_codeChange(e) {
				this.chargeback_warehouse_code = e.detail.value
			},
			// 退出登录
			logOut() {
				let _this = this
				uni.showModal({
					title: '温馨提示',
					content: '您确定要退出登录',
					success: function (res) {
						if (res.confirm) {
							console.log('用户点击确定');
							_this.$common.show('退出登录成功')
							setTimeout(() => {
								_this.$db.del('token')
								_this.$db.set('checkboxMobile_1',_this.$db.get('checkboxMobile'))
								_this.$common.guanbi('/pages/login/login')
							},1000)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	
</style>